package com.neutech.dao.impl;

import com.neutech.constant.Constant;
import com.neutech.dao.FoodDAO;
import com.neutech.entity.Food;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDAOlmpl implements FoodDAO {


    //使用apache 来执行查询语句
    private QueryRunner queryRunner = new QueryRunner(Constant.dataSource);

    //查询所有食品的实现方法
    @Override
    public List<Food> selectAllFood() {
        List<Food> foodList = new ArrayList<>();
        try {
            queryRunner.execute("select * from food", new ResultSetHandler<Object>() {

                //处理查询到的结果集
                @Override
                public Object handle(ResultSet resultSet) throws SQLException {
                    while (resultSet.next()){
                        //创建实体类Food 对象 food
                        Food food = new Food();
                        //调用food对象的set方法，将查询到的数据存入
                        food.setFoodId(resultSet.getInt("food_id"));
                        food.setFoodName(resultSet.getString("food_name"));
                        food.setFoodExplain(resultSet.getString("food_explain"));
                        food.setPrice(resultSet.getBigDecimal("food_price"));
                        food.setBusinessId(resultSet.getInt("business_id"));
                        //再将food对象中的值，添加到集合中
                        foodList.add(food);
                    }
                    return null;
                }
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return foodList;
    }

    //增加食品的方法实现
    @Override
    public int add(Food food) {
        int row = 0;
        try {
            row=queryRunner.execute("insert into food (food_name,food_explain,food_price,business_id) values (?,?,?,?)",
            food.getFoodName(),food.getFoodExplain(),food.getFoodPrice(),food.getBusinessId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    //通过食品名查找是否重复，方法实现
    @Override
    public Food selectFoodName(String foodName) {
        Food food = new Food();
        try {
            queryRunner.execute("select * from food where food_name = ?", new ResultSetHandler<Object>() {

                @Override
                public Object handle(ResultSet resultSet) throws SQLException {
                    //循环输出sql查询出来的结果集
                    while (resultSet.next()){
                        //food对象中的数据，为集合中查询出来的值
                       food.setFoodName(resultSet.getString("food_name"));
                       food.setFoodExplain(resultSet.getString("food_explain"));
                       food.setPrice(resultSet.getBigDecimal("food_price"));
                       food.setBusinessId(resultSet.getInt("business_id"));
                    }
                    return null;
                }
            },food.getFoodName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return food;
    }

}
